<template>
  <div class="custom_marquee" v-if="text">
    <div class="marquee-wrap">
      <div class="scroll">
        <p class="marquee">
          <a
            :href="txt.url"
            v-for="(txt, index) in text"
            :key="'text_index_' + index"
            :style="{ color: 'rgb' + $Plugins.rgb() }"
          >
            {{ txt.text }}
          </a>
        </p>
        <p class="copy">
          <a
            :href="txt.url"
            v-for="(txt, index) in text"
            :key="'text_index_' + index"
            :style="{ color: 'rgb' + $Plugins.rgb() }"
          >
            {{ txt.text }}
          </a>
        </p>
      </div>
      <p class="getWidth">
        <a
          :href="txt.url"
          v-for="(txt, index) in text"
          :key="'text_index_' + index"
          :style="{ color: 'rgb' + $Plugins.rgb() }"
        >
          {{ txt.text }}
        </a>
      </p>
    </div>
  </div>
</template>

<script>
export default {
  name: "marquee",
  props: {
    item: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      timer: null,
      text: "",
    };
  },
  created() {},
  mounted() {
    let timer = setTimeout(() => {
      this.move();
      clearTimeout(timer);
    }, 1000);
    this.text = this.$props.item.data;
  },
  methods: {
    move() {
      let maxWidth = document.querySelector(".marquee-wrap").clientWidth;
      let width = document.querySelector(".getWidth").scrollWidth;
      if (width <= maxWidth) return;
      let scroll = document.querySelector(".scroll");
      let distance = 0; // 位移距离
      this.timer = setInterval(() => {
        distance -= 1;
        if (-distance >= width) {
          distance = 16; // 距离必须与marquee的margin宽度相同
        }
        scroll.style.transform = "translateX(" + distance + "px)";
      }, 50);
    },
  },
  beforeDestroy() {
    clearInterval(this.timer);
  },
};
</script>

<style lang="less">
@base: 23.44rem;
.custom_marquee {
  padding: (5 / @base) (10 / @base);
  background-color: #fff;
}
.marquee-wrap {
  width: 100%;
  overflow: hidden;
  position: relative;
}
.marquee {
  margin-right: (14 / @base);
  a {
    padding: 0 (5 / @base);
  }
}
p {
  word-break: keep-all;
  white-space: nowrap;
  font-size: (14 / @base);
  font-family: "微软雅黑 Light";
  a {
    padding: 0 (5 / @base);
  }
}
.scroll {
  display: flex;
}
.getWidth {
  word-break: keep-all;
  white-space: nowrap;
  position: absolute;
  opacity: 0;
  top: 0;
}
</style>
